package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.module.domain.GpAccountManagement;
import com.gpframework.module.module.domain.req.GpAccountManagementAdd;
import com.gpframework.module.module.domain.req.GpAccountManagementReq;
import com.gpframework.module.module.domain.req.GpAccountManagementUpdate;
import com.gpframework.module.module.domain.vo.GpAccountManagementExcel;
import com.gpframework.module.module.domain.vo.GpAccountManagementVo;
import com.gpframework.module.module.service.IGpAccountManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpAccountManagementController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 账户管理Controller
 */
@Validated
@Api(tags = "账户管理")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/accountmanagement" )
public class GpAccountManagementController extends BaseController {

    private final IGpAccountManagementService iGpAccountManagementService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpAccountManagement
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:accountmanagement:list')")
    @GetMapping("/list")
    public TableDataInfo<GpAccountManagementVo> list(GpAccountManagementReq gpAccountManagementReq)
    {
        startPage();
        QueryWrapper<GpAccountManagement> lqw = new QueryWrapper<GpAccountManagement>();
        gpAccountManagementReq.generatorQuery(lqw,true);
        List<GpAccountManagementVo> list = iGpAccountManagementService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpAccountManagement
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:accountmanagement:add')" )
    @Log(title = "添加账户管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpAccountManagementAdd gpAccountManagementAdd) {
        GpAccountManagement gpAccountManagement = Convert.convert(new TypeReference<GpAccountManagement>() {}, gpAccountManagementAdd);
        return toAjax(iGpAccountManagementService.save(gpAccountManagement) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpAccountManagement
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:accountmanagement:edit')" )
    @Log(title = "修改账户管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpAccountManagementUpdate gpAccountManagementUpdate) {
        GpAccountManagement gpAccountManagement = Convert.convert(new TypeReference<GpAccountManagement>() {}, gpAccountManagementUpdate);
        return toAjax(iGpAccountManagementService.updateById(gpAccountManagement) ? 1 : 0);
    }


    /**
     * @Description 导出账户管理模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出账户管理模板")
    @Log(title = "导出账户管理模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpAccountManagementExcel> util = new ExcelUtil<>(GpAccountManagementExcel.class);
        return util.exportExcel(GpAccountManagementExcel.list(), "模板");
    }

    /**
     * @Description 导入账户管理
     * @Author gpframework
     * @Date 2023-10-27
     * @Param (GpMember
     * @Return TableDataInfo
     */
    @Log(title = "导入账户管理" , businessType = BusinessType.IMPORT)
    @PostMapping("/import" )
    @ApiOperation("导入账户管理")
    public AjaxResult entityImport(MultipartFile file, Long brandId, Long storeId) throws Exception {
        ExcelUtil<GpAccountManagementExcel> util = new ExcelUtil<GpAccountManagementExcel>(GpAccountManagementExcel. class);
        List<GpAccountManagementExcel> excelList = util.importExcel(file.getInputStream());

        return AjaxResult.builder(() -> {
            return this.iGpAccountManagementService.gpAccountManagementImport(excelList, brandId, storeId);
        });
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpAccountManagement
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:accountmanagement:export')" )
    @Log(title = "导出账户管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    @ApiOperation("导出账户管理")
    public AjaxResult export(GpAccountManagementReq gpAccountManagementReq) {
        QueryWrapper<GpAccountManagement> lqw = new QueryWrapper<GpAccountManagement>();
        gpAccountManagementReq.generatorQuery(lqw,true);
        List<GpAccountManagementVo> list = iGpAccountManagementService.listVo(lqw);
        ExcelUtil<GpAccountManagementVo> util = new ExcelUtil<GpAccountManagementVo>(GpAccountManagementVo. class);
        return util.exportExcel(list, "accountmanagement" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpAccountManagement
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:accountmanagement:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpAccountManagementVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpAccountManagementService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpAccountManagement
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:accountmanagement:remove')" )
    @Log(title = "删除账户管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpAccountManagementService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
